package com.zs.blog.controller;

import com.zs.blog.model.ResponseResult;
import com.zs.blog.model.dto.ArticleDTO;
import com.zs.blog.model.vo.*;
import com.zs.blog.service.IArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Title: ArticleController
 * @Author zs
 * @Package com.zs.blog.controller
 * @Date 2024/10/13 19:39
 * @description: 文章接口层
 */
@RestController
@Tag(name = "文章相关接口")
public class ArticleController {

    public IArticleService articleService;

    @Autowired
    private ArticleController(IArticleService articleService) {
        this.articleService = articleService;
    }

    @GetMapping("/admin/article/list")
    @Operation(summary = "获取分页的文章列表")
    public ResponseResult<PageInfoVO<ArticleListVO>> listArticleWithPagination(Integer pageNum, Integer pageSize) {
        PageInfoVO<ArticleListVO> listArticle = articleService.listArticleWithPagination(pageNum, pageSize);
        return ResponseResult.success(listArticle);
    }

    @PostMapping("/admin/article/publish")
    @Operation(summary = "发布文章")
    public ResponseResult<?> publishArticle(@RequestBody ArticleDTO articleDTO) {
        articleService.publish(articleDTO);
        return ResponseResult.success();
    }

    @DeleteMapping("/admin/article/delete")
    @Operation(summary = "删除文章")
    public ResponseResult<?> deleteArticle(@RequestBody List<Long> articleIdList) {
        articleService.deleteArticle(articleIdList);
        return ResponseResult.success();
    }

    @PutMapping("/admin/article/update/state")
    @Operation(summary = "修改文章状态")
    public ResponseResult<?> updateArticleState(@RequestParam Long articleId, @RequestParam Integer articleState) {
        articleService.updateArticleState(articleId, articleState);
        return ResponseResult.success();
    }

    @PutMapping("/admin/article/update/topstate")
    @Operation(summary = "修改文章置顶状态")
    public ResponseResult<?> updateArticleTopState(@RequestParam Long articleId, @RequestParam Integer isTop) {
        articleService.updateArticleTopState(articleId, isTop);
        return ResponseResult.success();
    }

    @GetMapping("/admin/article/{articleId}")
    @Operation(summary = "根据文章ID获取文章数据")
    public ResponseResult<ArticleEditVO> getArticleByArticleId(@PathVariable Long articleId) {
        ArticleEditVO article = articleService.getArticleByArticleId(articleId);
        return ResponseResult.success(article);
    }


    @GetMapping("/portal/article/random")
    @Operation(summary = "获取随机文章")
    public ResponseResult<List<ArticleRandomVO>> listRandomArticle() {
        List<ArticleRandomVO> listRandomArticle = articleService.listRandomArticle();
        return ResponseResult.success(listRandomArticle);
    }

    @GetMapping("/portal/article/detail/{articleId}")
    @Operation(summary = "根据文章ID获取文章详情")
    public ResponseResult<ArticleDetailVO> getArticleDetailByArticleId(@PathVariable Long articleId) {
        ArticleDetailVO articleDetailVO = articleService.getArticleDetailByArticleId(articleId);
        return ResponseResult.success(articleDetailVO);
    }

    @GetMapping("/portal/article/recommend")
    @Operation(summary = "获取推荐文章")
    public ResponseResult<List<ArticleRecommendVO>> getRecommendArticle() {
        List<ArticleRecommendVO> articleRecommendList = articleService.getRecommendArticle();
        return ResponseResult.success(articleRecommendList);
    }

    @GetMapping("/portal/article/related/{articleId}/{categoryId}")
    @Operation(summary = "获取相关文章")
    public ResponseResult<List<ArticleRelatedVO>> getRelatedArticle(@PathVariable Long articleId, @PathVariable Long categoryId) {
        List<ArticleRelatedVO> articleRelatedList = articleService.getRelatedArticle(articleId, categoryId);
        return ResponseResult.success(articleRelatedList);
    }


    @GetMapping("/portal/article/timeline")
    @Operation(summary = "获取文章时间轴")
    public ResponseResult<List<ArticleTimelineVO>> listArticleTimeline() {
        List<ArticleTimelineVO> articleTimelineList = articleService.listArticleTimeline();
        return ResponseResult.success(articleTimelineList);
    }

    @GetMapping("/portal/article/list")
    @Operation(summary = "获取文章列表-前台")
    public ResponseResult<PageInfoVO<ArticleListVO>> listArticleWithPaginationForPortal(Integer pageNum, Integer pageSize) {
        PageInfoVO<ArticleListVO> articleListWithPage = articleService.listArticleWithPagination(pageNum, pageSize);
        return ResponseResult.success(articleListWithPage);
    }

}
